<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<jsp:include page="../../common.jsp"></jsp:include>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>借调登记表</title>
	
<script type="text/javascript">
$(function () {

    //1.初始化Table
    var oTable = new TableInit();
    oTable.Init();

    //2.初始化Button的点击事件
    var oButtonInit = new ButtonInit();
    oButtonInit.Init();
    
	//初始化提示框的位置：top-center，bottom-left，bottom-right
    toastr.options.positionClass = 'toast-top-center';
	
    $("#cremark3").datetimepicker({
    	/* 显示格式 */       
		format: "yyyy-mm-dd",
	    /* 完成关闭 */
	    autoclose: true,
	   /*  今天 时间按钮*/
	    todayBtn: true, 
	   /*  今天的时间变黄色 */
	    todayHighlight:true, 
	    /* 定位 */
	    pickerPosition: "bottom-left",
	    /* 最小值 */
	    minuteStep: 10,
	    /*  语言 */
	    language: 'zh-CN',  
		/* 分上午下午 */
	    showMeridian: true,
	    /* 分的所有 */
	    minuteStep:true,
	    /* 月视图 */
	    startView: 2,
	    /* 日期时间选择器所能够提供的最精确的时间选择视图 */
		minView: 2
	}).on('hide',function(e) {
		e.stopPropagation();
		$('#fm').data('bootstrapValidator').updateStatus('cremark3', 'NOT_VALIDATED',null)
			.validateField('cremark3'); 
	});
    
    $("#cremark3c").datetimepicker({
		format: "yyyy-mm-dd",
	    autoclose: true,
	    todayBtn: true, 
	    todayHighlight:true, 
	    pickerPosition: "bottom-left",
	    minuteStep: 10,
	    language: 'zh-CN',  
	    showMeridian: true,
	    minuteStep:true,
	    startView: 2,
		minView: 2
	});
    
    $("#cremark7").datetimepicker({
		format: "yyyy-mm-dd",
	    autoclose: true,
	    todayBtn: true, 
	    todayHighlight:true, 
	    pickerPosition: "bottom-left",
	    minuteStep: 10,
	    language: 'zh-CN',  
	    showMeridian: true,
	    minuteStep:true,
	    startView: 2,
		minView: 2
	}).on('hide',function(e) {
		e.stopPropagation();
		$('#fm').data('bootstrapValidator').updateStatus('cremark7', 'NOT_VALIDATED',null)
			.validateField('cremark7'); 
	});
    
    $("#cremark5").datetimepicker({
		format: "yyyy-mm-dd",
	    autoclose: true,
	    todayBtn: true, 
	    todayHighlight:true, 
	    pickerPosition: "bottom-left",
	    minuteStep: 10,
	    language: 'zh-CN',  
	    showMeridian: true,
	    minuteStep:true,
	    startView: 2,
		minView: 2
	}).on('hide',function(e) {
		e.stopPropagation();
		$('#fm').data('bootstrapValidator').updateStatus('cremark5', 'NOT_VALIDATED',null)
			.validateField('cremark5'); 
	});
   
	
	//模态框关闭事件
    $('#exampleModal').on('hide.bs.modal',function(){
    	//表单清空
    	$("#fm")[0].reset();
    	//表单验证清空
    	$('#fm').data('bootstrapValidator').resetForm(true);
    });
    
    
  	//jQuery，既阻止默认行为又停止冒泡  导入按钮
    $("#ss").on('click',function(){
    	    return false;//当然 也阻止了事件本身
    });
  	
  	//调用下拉框多选初始化方法
   // getPlan();

});

 /* function getPlan() {
	//清空下拉框节点
	$("#cremark9").empty();
	$.ajax({
		type : "post",
		url : "#",
		success : function(data) {
			var str = "";
			var json = eval('(' + data + ')');
			for (var i = 0; i < json.length; i++) {
				str += '<option value="'+json[i].model+'">'
						+ json[i].model + '</option>';
			}
			$("#cremark9").append(str);
			$("#cremark9").selectpicker('refresh');
		}
	});
}  */


var TableInit = function () {
    var oTableInit = new Object();
    //初始化Table
    oTableInit.Init = function () {
        $('#tb_departments').bootstrapTable({
        	url: '${pageContext.request.contextPath }/loan/selectLoanList.do',         //请求后台的URL（*）
            method: 'get',                      //请求方式（*）
            toolbar: '#toolbar',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            sortable: false,                     //是否启用排序
            sortOrder: "asc",                   //排序方式
            queryParams: oTableInit.queryParams,//传递参数（*）
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber:1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 20, 30],        //可供选择的每页的行数（*）
            strictSearch: true,
            minimumCountColumns: 2,             //最少允许的列数
            clickToSelect: true,                //是否启用点击选中行
            height: 550,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "loanId",                     //每一行的唯一标识，一般为主键列
            detailView: false,                   //是否显示父子表
            columns:  [{
                checkbox: true
            }, {
                field: 'loanId',
                title: '借调登记id',
                visible: false
            }, {
                field: 'cremark9',
                title: '物资型号'
            }, {
                field: 'cremark1',
                title: '数量'
            } , {
                field: 'cremark2',
                title: '借调人'
            } , {
                field: 'cremark3',
                title: '借调日期'
            }, {
                field: 'cremark4',
                title: '经手人'
            }, {
                field: 'cremark5',
                title: '预计归还日期'
            }, {
                field: 'cremark6',
                title: '归还人'
            }, {
                field: 'cremark7',
                title: '实际归还日期'
            }, {
                field: 'cremark8',
                title: '备注'
            }]
        });
    };

    //得到查询的参数
    oTableInit.queryParams = function (params) {
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
      		limit: params.limit,   //页面大小
            offset: params.offset/params.limit+1,  //页码
            cremark9:$.trim($('#cremark9c').val()), 
            cremark3:$("#cremark3c").find("input").val(), 
        };
        return temp;
    };
    return oTableInit;
};


var ButtonInit = function () {
    var oInit = new Object();
    var postdata = {};

    oInit.Init = function () {
        //初始化页面上面的按钮事件
    };

    return oInit;
};

//刷新表格
function refresh(){
	$("#tb_departments").bootstrapTable("refreshOptions",{pageNumber:1})
};

//删除
function doDelete() {
	var row=$.map($("#tb_departments").bootstrapTable('getSelections'),function(row){
		return row ;
	});
	if (row.length == 0) {
		toastr.warning('请至少选择一条数据!');
	}else{
		var ids="";
		var names="";
		for(var i=0;i<row.length;i++){
			if(i==0 || i=="0"){
				ids+=row[i].loanId;
			}else{
				ids+=","+row[i].loanId;
			}
		}
		
		//询问框
		Ewin.confirm({ message: "确认要删除选择的数据吗？" }).on(function (e) {
	       if (!e) {
	         return;
	       } 
	    	var url = "${pageContext.request.contextPath }/loan/deleteLoan.do";
		    $.post(url, "ids="+ids, function(data) {
				if (data >= 1) {
					toastr.success('删除成功');
					setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
						$('#tb_departments').bootstrapTable('refresh');
						refresh()
					},1000);
				} else {
					toastr.error('删除失败');
					setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
						$('#tb_departments').bootstrapTable('refresh');
						refresh()
					},1000);
				}
			}); 
	    }); 
	}
};

//打开添加模态框
function doAdd() {
	$("#bt").html("新增借调记录");
	$("#loanId").val("");
	$("#cremark9").selectpicker("val","");
	addFig  =  false
	$('#exampleModal').modal(open);
};

//修改赋值
function doView() {
	//取表格的选中行数据
    var row = $("#tb_departments").bootstrapTable('getSelections');
	if (row.length == 1) {
		$("#bt").html("修改借调记录");
		$("#loanId").val(row[0].loanId);
		$('#cremark9').selectpicker('val',(row[0].cremark9));
		$("#cremark1").val(row[0].cremark1);
		$("#cremark2").val(row[0].cremark2);
		$("#cremark3").val(row[0].cremark3);
		$("#cremark4").val(row[0].cremark4);
		$("#cremark5").val(row[0].cremark5);
		$("#cremark6").val(row[0].cremark6);
		$("#cremark7").val(row[0].cremark7);
		$("#cremark8").val(row[0].cremark8);
		addFig  =  false
		$('#exampleModal').modal(open);
	}else{
		toastr.warning('请选择一条数据!');
	}
};

//保存
addFig  =  false
function save() {

	if(addFig == true){
		return false;
	}
	addFig  =  true;
	
    var bootstrapValidator = $("#fm").data('bootstrapValidator');  
    //开启验证
    bootstrapValidator.validate();  
    if (bootstrapValidator.isValid()){
    	if (document.getElementById('loanId').value=='') {
			var url = "${pageContext.request.contextPath }/loan/insertLoan.do";
			var title="新增";
    	} else {
			var url = "${pageContext.request.contextPath }/loan/editLoan.do";
			var title="修改";
    	}  
	    $.post(url, $("#fm").serialize(), function(data) {
            if (data == 1) {
                toastr.success(title+'成功');
                setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
                    $('#exampleModal').modal('hide');
                    $('#tb_departments').bootstrapTable('refresh');
                    refresh();
                    addFig  =  false
                },1000);
            } else {
                toastr.error(title+'失败');
                addFig  =  false
            }
        });
    }else{
	    addFig  =  false
	    return;  
    } 
};

function seach() {
	$('#tb_departments').bootstrapTable('refresh'); 
}
</script>
</head>
<body>
	<div class="panel-body" style="padding-bottom:0px;">
        <div class="panel panel-default">
            <div class="panel-heading">查询条件</div>
            <div class="panel-body">
                <form id="formSearch" class="form-horizontal">
                    <div class="form-group" style="margin-top:15px">
                    	<label class="control-label col-sm-1" for="txt_search_statu">借调物资</label>
                        <div class="col-sm-3">
                        	 <input type="text" class="form-control" id="cremark9c">
	                     </div>
                        <label class="control-label col-sm-1" for="txt_search_departmentname">借调日期 </label>
                        <div class="col-sm-3">
                            <input type="text" id="cremark3c" placeholder="yyyy-MM-dd" 
								readonly="readonly" class="form_datetime form-control">
                        </div>
                        <div class="col-sm-4" style="text-align:left;">
	                        <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary" onclick="seach()">查询</button>
	                    </div>
                     </div>
                </form>
            </div>
        </div>       

         <div id="toolbar" class="btn-group">
            <button id="btn_add" type="button" class="btn btn-default" onclick="doAdd()">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            <button id="btn_edit" type="button" class="btn btn-default" onclick="doView()">
           		 <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
            </button>
            <button id="btn_delete" type="button" class="btn btn-default" onclick="doDelete()">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
            </button>
        </div>
        <table id="tb_departments"></table>
    </div>
    
    
      <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
   		<div class="modal-dialog" role="document">
    		<div class="modal-content" style="width: 700px;">
      			<div class="modal-header">
        			<button type="button" class="close closefm" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        			<h4 class="modal-title" id="exampleModalLabel">
        				<span id="bt" style="font-size:13px"></span>
        			</h4>
      			</div>
      			<div class="modal-body">
	        		<form id="fm" class="form-horizontal">
	          			<div class="form-group">
	           				 <label class="col-sm-3 control-label">借调物资:</label>
	            			 <div class="col-sm-7"> 
		            			 <select id="cremark9" name="cremark9" class="selectpicker show-tick form-control">
								 </select>
		            			 <input type="hidden" id="loanId" name="loanId" >
	          				</div>
	          			</div>
	          			<div class="form-group">
	           				<label  class="col-sm-3 control-label">数量:</label>
							<div class="col-sm-7"> 
								<input type="text" class="form-control" id="cremark1" name="cremark1"  placeholder="请输入" autocomplete="off">
	          					<span class="xing"></span>
	          				</div>
	          			</div>
	          			<div class="form-group">
	           				 <label  class="col-sm-3 control-label">借调人:</label>
							<div class="col-sm-7"> 
								<input type="text" class="form-control" id="cremark2" name="cremark2"  placeholder="请输入" autocomplete="off">
	          					<span class="xing"></span>
	          				</div>
	          			</div>
	          			
	          			<div class="form-group">
	          				<label class="col-sm-3 control-label">借调日期:</label>
				            <div class="col-sm-7"> 
								<input type="text" name="cremark3" id="cremark3" placeholder="yyyy-MM-dd" 
								readonly="readonly" class="form_datetime form-control">
					            <span class="xing"></span>
	          				</div>
	          			</div>
	          			
	          			<div class="form-group">
	          				<label class="col-sm-3 control-label">经手人:</label>
				            <div class="col-sm-7"> 
					            <input type="text" class="form-control" id="cremark4" name="cremark4"  placeholder="请输入">
					            <span class="xing"></span>
	          				</div>
	          			</div>
	          			
	          			<div class="form-group">
	          				<label class="col-sm-3 control-label">预计归还日期:</label>
				            <div class="col-sm-7"> 
				            	<input type="text" name="cremark5" id="cremark5" placeholder="yyyy-MM-dd" 
								readonly="readonly" class="form_datetime form-control">
					            <span class="xing"></span>
	          				</div>
          				</div>
          				
          				<div class="form-group">
	          				 <label  class="col-sm-3 control-label">归还人:</label>
							<div class="col-sm-7"> 
								<input type="text" class="form-control" id="cremark6" name="cremark6"  placeholder="请输入" autocomplete="off">
	          				</div>
	          			</div>
	          			
	          			<div class="form-group">
	          				 <label  class="col-sm-3 control-label">实际归还日期:</label>
							<div class="col-sm-7"> 
								<input type="text" name="cremark7" id="cremark7" placeholder="yyyy-MM-dd" 
								readonly="readonly" class="form_datetime form-control">
	          				</div>
          				</div>
          				
          				<div class="form-group">
	          				 <label  class="col-sm-3 control-label">备注:</label>
							<div class="col-sm-7"> 
								<input type="text" class="form-control" id="cremark8" name="cremark8"  placeholder="请输入" autocomplete="off">
	          				</div>
	          			</div>
	          			
	          			 <div class="modal-footer">
                           		<button type="button" class="btn btn-default closefm" data-dismiss="modal" >关闭</button>
						        <button type="button"  class="btn btn-primary" onclick="save()">保存</button>
                        </div>
	          			
	        		</form>
	      		</div>
    		</div>
  		</div>
	</div>
	
	<script type="text/javascript">
	$(function() {
		
		$("#fm").bootstrapValidator({
			feedbackIcons : {
				valid : 'glyphicon glyphicon-ok',
				invalid : 'glyphicon glyphicon-remove',
				validating : 'glyphicon glyphicon-refresh'
			},
			fields : {
				cremark1 : {
					validators : {
						notEmpty : {
							message : '数量不能为空'
						},
						regexp: {
	                         regexp: /^[1-9]\d*$/,
	                         message: '请输入正整数'
	                    }
					}
				},
				
				cremark2 : {
					validators : {
						notEmpty : {
							message : '借调人不能为空'
						},
					}
				},
				
				cremark3 : {
					validators : {
						notEmpty : {
							message : '借调日期不能为空'
						},
					}
				},
				
				cremark4 : {
					validators : {
						notEmpty : {
							message : '经手人不能为空'
						},
					}
				},
				
				cremark5 : {
					validators : {
						notEmpty : {
							message : '预计归还日期不能为空'
						},
					}
				},
			
			},
		});
	});
	</script>
</body>
</html>